#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int port=7777;
int main()
{
	int sock_descriptor;

	int size_of_addr;
	char buf[8192]={0};
	int i, length;

	//创建socket文件描述符出啦
	sock_descriptor=socket(AF_INET, SOCK_STREAM, 0);
	if(sock_descriptor == -1)
	{
		perror("socket!");
		exit(1);
	}

	//搞出来地址信息
	struct sockaddr_in sin;
	bzero(&sin, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons(port);

	//接下来要绑定本地的地址、端口，和socket文件描述符绑定
	//int bind (int sockfd, struct sockaddr* addr, int addrLen);
	if(bind(sock_descriptor, (struct sockaddr*)&sin, sizeof(sin)) == -1)
	{
		perror("bind");
		exit(1);
	}

	//侦听
	if(listen(sock_descriptor, 20) == -1)
	{
		perror("listen1");
		exit(1);
	}

	printf("waiting for accepting!\n");

	while(1)
	{
		struct sockaddr_in pin;
		int temp_sock_descriptor;
		temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr*)&pin, &size_of_addr);
		if(temp_sock_descriptor == -1)
		{
			perror("accept!");
			exit(1);
		}

		if(recv(temp_sock_descriptor, buf, sizeof(buf), 0) == -1)
		{
			perror("recv()!");
			exit(1);
		}
		printf("received : %s\n", buf);
		memset(buf, 0, 8192);
/*      	length = strlen(buf);

		for(i  = 0 ; i < length; i++)
		{
			buf[i] = toupper(buf[i]);
		}
		if(send(temp_sock_descriptor, buf, length, 0) == -1)
		{
			perror("send()!");
			exit(1);
		}
*/
		close(temp_sock_descriptor);
	}
}
